//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.configuration;

import com.wsd.smartcarwasher.interceptor.CORSAuthenticationFilter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.password}")
    private String password;

    public ShiroConfig() {
    }


    public CORSAuthenticationFilter corsAuthenticationFilter(){
        return new CORSAuthenticationFilter();
    }

   /* @Bean
    public FilterRegistrationBean registration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new CORSAuthenticationFilter());
        registration.setEnabled(false);
        return registration;
    }*/

    @Bean
    public ShiroFilterFactoryBean shiroFactory(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //自定义过滤器
        Map<String, Filter> filterMap = shiroFilterFactoryBean.getFilters();
        filterMap.put("authc", corsAuthenticationFilter());
        //不需要登陆认证的
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap();
        //filterChainDefinitionMap.put("/v1/logout", "logout");
        filterChainDefinitionMap.put("/v1/login_by_password", "anon");
        filterChainDefinitionMap.put("/v1/login_by_weixin", "anon");
        filterChainDefinitionMap.put("/v1/loginOut", "anon");
        filterChainDefinitionMap.put("/v1/wx/auth_code", "anon");
        filterChainDefinitionMap.put("/v1/wx/official_account_login", "anon");
        filterChainDefinitionMap.put("/v1/wx_login_by_mobile", "anon");
        filterChainDefinitionMap.put("/v1/wx/sessionKey", "anon");
        filterChainDefinitionMap.put("/v1/login", "anon");
        filterChainDefinitionMap.put("/v1/wx_machines", "anon");
        filterChainDefinitionMap.put("/v1/pay/call_back", "anon");
        filterChainDefinitionMap.put("/v1/pay/prepay", "anon");
        filterChainDefinitionMap.put("/v1/machine/detail", "anon");
        filterChainDefinitionMap.put("/v1/wash_order", "anon");
        filterChainDefinitionMap.put("/v1/getVerify", "anon");
        filterChainDefinitionMap.put("/v1/coupon/exportExcel", "anon");
        filterChainDefinitionMap.put("/v1/prorate/test", "anon");
//        filterChainDefinitionMap.put("/v2/api-docs","anon");

        //需要拦截的
//        filterChainDefinitionMap.put("/v1/**","authc");
        filterChainDefinitionMap.put("/sys/**","authc");
        filterChainDefinitionMap.put("/swagger-ui.html/**","authc");
        filterChainDefinitionMap.put("/common/**","authc");
        //要拦截的url
        //filterChainDefinitionMap.put("/v1/add_coupon", "corsAuthenticationFilter");
        //filterChainDefinitionMap.put("/v1/sms/testKey", "corsAuthenticationFilter");
        //filterChainDefinitionMap.put("/v1/machines", "corsAuthenticationFilter");
//        filterChainDefinitionMap.put("/sys/menu/getUserMenus", "corsAuthenticationFilter");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }

    @Bean
    public ShiroRealm myShiroRealm() {
        ShiroRealm myShiroRealm = new ShiroRealm();
        myShiroRealm.setCredentialsMatcher(this.hashedCredentialsMatcher());
        return myShiroRealm;
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setSessionManager(this.sessionManager());
        securityManager.setCacheManager(this.cacheManager());
        securityManager.setRealm(this.myShiroRealm());
        return securityManager;
    }

    @Bean
    public SessionManager sessionManager() {
        MySessionManager mySessionManager = new MySessionManager();
        mySessionManager.setSessionDAO(this.redisSessionDAO());
        //mySessionManager.setGlobalSessionTimeout(20000);
        return mySessionManager;
    }

    public RedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        redisManager.setHost(this.host + ":" + this.port);
        redisManager.setTimeout(this.timeout);
        redisManager.setPassword(this.password);
        return redisManager;
    }

    @Bean
    public RedisCacheManager cacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(this.redisManager());
        redisCacheManager.setExpire(36000);
        redisCacheManager.setPrincipalIdFieldName("id");
        return redisCacheManager;
    }

    @Bean
    public RedisSessionDAO redisSessionDAO() {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(this.redisManager());
        return redisSessionDAO;
    }

    @Bean(
            name = {"ExceptionHandler"}
    )
    public HandlerExceptionResolver handlerExceptionResolver() {
        return new MyExceptionHandler();
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    /*@Bean
    public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }*/

  /*  @Bean
    public  Timer getTimeTask(TimeTaskService timeTaskService){
        //设置执行时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
        //定制每天的21:09:00执行，
        calendar.set(year, month, day, 21, 54, 00);
        Date date = calendar.getTime();
        Timer timer = new Timer();
        //System.out.println(date);
        int period = 2 * 1000;
        //每天的date时刻执行task，每隔2秒重复执行
        timer.schedule(timeTaskService, date);
        //每天的date时刻执行task, 仅执行一次
        //timer.schedule(task, date);
        return timer;
    }*/
}
